#pragma once

#include<functional>
namespace Queue
{
    template<class T, class Container = deque<T>>
    class queue
    {
    public:
        queue()
            : _con()
        { }

        void push(const T& x)
        {
            _con.push_back(x);
        }

        void pop()
        {
            _con.pop_front();
        }

        T& back()
        {
            return _con.back();
        }

        const T& back()const
        {
            return _con.back();
        }

        T& front()
        {
            return _con.front();
        }

        const T& front()const
        {
            return _con.front();
        }

        size_t size()const
        {
            return _con.size();
        }

        bool empty()const
        {
            return _con.empty();
        }

    private:
        Container _con;
    };

    template<class T, class Container = vector<T>,class Compare = less<T>>
    class priority_queue
    {
    public:
        priority_queue()
            :_con()
            ,_com()
        { }

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first < last)
            {
                push(*first);
                first++;
            }
        }

        bool empty() const
        {
            return size() == 0;
        }

        size_t size() const
        {
            return _con.size();
        }

        const T& top() const
        {
            return _con[0] ;
        }

        void adjust_up(int child)
        {
            int parent = (child - 1) / 2;

            while (child > 0)
            {
                if (_com(_con[parent], _con[child]))
                {
                    std::swap(_con[parent], _con[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }
        void push(const T& x)
        {
            _con.push_back(x);

            adjust_up(size() - 1);
        }

        void adjust_down(int parent)
        {
            int child = parent * 2 + 1;
            while (child < _con.size())
            {
                if (child + 1 < _con.size() && _com(_con[child], _con[child + 1]))
                {
                    child += 1;
                }

                if (_com(_con[parent], _con[child]))
                {
                    std::swap(_con[parent], _con[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }
        void pop()
        {
            std::swap(_con[0], _con[_con.size() - 1]);
            _con.pop_back();
            adjust_down(0);
        }


    private:
        Container _con;
        Compare _com;
    };
}